$( document ).ready(function() {
	$("#messageDiv").empty();
	$("#messageDiv").hide();
	$("[name='password']").val("");
});

function checkPassword() {
	$.getJSON("patient/checkPassword.html?password="+$("[name='password']").val(), function(data) {
		if(data.passwordConfirm==true){
			$("#requirePw").hide();
			$("#messageDiv").empty();
			$("#messageDiv").hide();
			$("#recordDetail").empty();
			$("#recordDetailView").empty();
			$("#recordDetailView").hide();
			$.getJSON("patient/recordList.html", function(result){
				if(!jQuery.isEmptyObject(result.recordDetailList)){
					$("#recordDetail").data("recordDetails", result);
					printRecordTableBody(result);
					$("#recordDetail").append("<br /><button id=\"confirmRecord\" onclick=\"confirmRecord()\" class=\"button\" class=\"button\">confirm</button>");
				}else{
					$("#recordDetail").append("<strong>No result.</strong>");
				}
			});
		}else{
			$("#messageDiv").removeClass("success");
			$("#messageDiv").removeClass("error");
			$("#messageDiv").addClass("error");
			$("#messageDiv").html("<strong>Incorrect password</strong>");
			$("#messageDiv").show();
		}
	});
}

function printRecordTableBody(data) {
	
	$("#recordDetail")
			.append(
					"<table id=\"list\" class=\"tablecss\">" +
					"<thead>" +
					"<tr>" +
					"<th></th>"+
					"<th>Record id</th>"+
					"<th>Patient id</th>" +
					"<th>Patient name</th>" +
					"<th>Doctor id</th>" +
					"<th>Doctor name</th>" +
					"<th>Start time</th>" +
					"<th>End time</th>" +
					"</tr>" +
					"</thead>" +
					"<tbody id=\"tableBody\">" +
					"</tbody>" +
					"</table>");
	var tr;
	$(data).each(
			function(index, value) {
				$(value.recordDetailList).each(function (i,v){
					tr = $("<tr>");
					tr.append("<td><input type=\"radio\" name=\"recordRadio\" value=\""+v.recordId+"\"></td>");
					tr.append("<td>" + v.recordId + "</td>");
					tr.append("<td>" + v.patientId + "</td>");
					tr.append("<td>" + v.patientName + "</td>");
					tr.append("<td>" + v.doctorId + "</td>");
					tr.append("<td>" + v.doctorName + "</td>");
					tr.append("<td>" + convertDateTime(v.startTime) + "</td>");
					tr.append("<td>" + convertDateTime(v.endTime) + "</td>");
					tr.append("</tr>");
					$("#tableBody").append(tr);
				});
				
			});
	$("#list").tablesorter({
		widgets : [ 'zebra' ],
		headers : {
			0 : {
				sorter : false
			}
		}
	});
}
function confirmRecord(){
	var id=$("[name=\"recordRadio\"]:checked").val();
	$("#patientResult").empty();
	$("#recordDetailView").empty();
	$("#recordDetailView").hide();
	var record=null;
	var recordList=$("#recordDetail").data("recordDetails");
	$(recordList).each(function(index, value){
		$(value.recordDetailList).each(function (i,v){
			if(v.recordId==id){
				record=v;
			}
		});
	});
	if(record!=null){
		$("#recordDetailView").append("<h1>Visit Record</h1><label for=\"rdRecordId\">Record id</label>" +
				"<textarea id=\"rdRecordId\" rows=\"1\" cols=\"10\" readonly>"+record.recordId+"</textarea><br />" +
						"<label for=\"rdPatientId\">Patient id</label>" +
				"<textarea id=\"rdPatientId\" rows=\"1\" cols=\"10\" readonly>"+record.patientId+"</textarea><br />" +
						"<label for=\"rdPatientName\">Patient name</label>" +
				"<textarea id=\"rdPatientName\" rows=\"1\" cols=\"30\" readonly>"+record.patientName+"</textarea><br />" +
						"<label for=\"rdDoctorId\">Doctor id</label>" +
				"<textarea id=\"rdDoctorId\" rows=\"1\" cols=\"10\" readonly>"+record.doctorId+"</textarea><br />" +
						"<label for=\"rdDoctorName\">Doctor name</label>" +
				"<textarea id=\"rdDoctorName\" rows=\"1\" cols=\"30\" readonly>"+record.doctorName+"</textarea><br />"+
						"<label for=\"rdStartTime\">Start time</label>" +
				"<textarea id=\"rdStartTime\" rows=\"1\" cols=\"30\" readonly>"+convertDateTime(record.startTime)+"</textarea><br />"+
						"<label for=\"rdEndTime\">End time</label>" +
				"<textarea id=\"rdEndTime\" rows=\"1\" cols=\"30\" readonly>"+convertDateTime(record.endTime)+"</textarea><br />"+
						"<label for=\"rdDiagnosis\">Diagnosis</label>" +
				"<textarea id=\"rdDiagnosis\" rows=\"5\" cols=\"50\" readonly>"+record.diagnosis+"</textarea><br />" +
						"<label for=\"rdDrug\">Drug</label>" +
				"<textarea id=\"rdDrug\" rows=\"5\" cols=\"50\" readonly>"+record.drug+"</textarea><br />" +
						"<label for=\"rdInstruction\">Instruction</label>" +
				"<textarea id=\"rdInstruction\" rows=\"5\" cols=\"50\" readonly>"+record.instruction+"</textarea><br />" +
						"<label for=\"rdDEAnum\">DEA number</label>" +
				"<textarea id=\"rdDEAnum\" rows=\"5\" cols=\"50\" readonly>"+record.deaNum+"</textarea><br />" +
						"<label for=\"rdTreatSche\">Treatment Schedule</label>" +
				"<textarea id=\"rdTreatSche\" rows=\"5\" cols=\"50\" readonly>"+record.treatmentSchedule+"</textarea><br />");
		$("#recordDetailView").show();
	}
}

function convertDateTime(milliSec){
	var dateTime=new Date(milliSec);
	var format=new String("");
	format=dateTime.getFullYear()+"-";
	if((dateTime.getMonth()+1)<10){
		format=format+"0"+(dateTime.getMonth()+1)+"-";
	}else{
		format=format+(dateTime.getMonth()+1)+"-";
	}
	if(dateTime.getDate()<10){
		format=format+"0"+dateTime.getDate()+" ";
	}else{
		format=format+dateTime.getDate()+" ";
	}
	if(dateTime.getHours()<10){
		format=format+"0"+dateTime.getHours()+":";
	}else{
		format=format+dateTime.getHours()+":";
	}
	if(dateTime.getMinutes()<10){
		format=format+"0"+dateTime.getMinutes();
	}else{
		format=format+dateTime.getMinutes();
	}
	return format;
}
